package chapter1.No1_2_使用多线程.c实例变量于线程安全;

/**
 * *：多个线程同时访问同一个变量
 *      i--的操作要分成如下3步：
 *          1. 取得原有的i
 *          2. i-1
 *          3. 将i进行赋值
 *      所以i--是会引发线程安全的问题
 *
 * @author liujian
 * @version 2016/11/8 0008
 */
public class T2_MyThread extends Thread {
    int count = 5;
    @Override
    public void run() {
        count--;
        System.out.println(Thread.currentThread().getName()+" 计算，count="+count);
    }
//    // 使用synchronized关键字，同步
//    synchronized public void run() {
//        count--;
//        System.out.println(Thread.currentThread().getName()+" 计算，count="+count);
//    }

    public static void main(String[] args) {
        T2_MyThread myThread = new T2_MyThread();
        Thread t1 = new Thread(myThread, "A");
        Thread t2 = new Thread(myThread, "B");
        Thread t3 = new Thread(myThread, "C");
        Thread t4 = new Thread(myThread, "D");
        Thread t5 = new Thread(myThread, "E");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();

    }
}
